Prettify the output by running it through xmllint --format if xmllint is
authorJohan Dahlin <jdahlin@async.com.br>
Thu, 28 Jun 2007 15:15:02 +0000 (15:15 +0000)
committerJohan Dahlin <johan@src.gnome.org>
Thu, 28 Jun 2007 15:15:02 +0000 (15:15 +0000)
2007-06-28  Johan Dahlin  <jdahlin@async.com.br>

    * gtk/gtk-builder-convert (_indent): Prettify the output by
    running it through xmllint --format if xmllint is available
    which also requires the subprocess module only available
    in python 2.4 or later

svn path=/trunk/; revision=18281

ChangeLog
gtk/gtk-builder-convert

index 69f8dff9c8e9b29455ed405c1ce47e37cd201120..ef25513526d8bf1584e9de8d0df521a36192e558 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2007-06-28  Johan Dahlin  <jdahlin@async.com.br>
+
+       * gtk/gtk-builder-convert (_indent): Prettify the output by
+       running it through xmllint --format if xmllint is available 
+       which also requires the subprocess module only available 
+       in python 2.4 or later
+
 2007-06-28  Christian Persch  <chpe@gnome.org>
 
        * gtk/gtkvolumebutton.c: (gtk_volume_button_class_init),
index 7237c8fb2e3215ee64403e7205ea4debdb6e0b60..be33edf4e63f311c9d517236e47a34ea569edb88 100644 (file)
 #  GtkTextView.text -> GtkTextBuffer
 #  Toolbars
 
+import os
 import sys
 
 from xml.dom import minidom, Node
 
+# The subprocess is only available in Python 2.4+
+try:
+    import subprocess
+    subprocess # pyflakes
+except ImportError:
+    subprocess = None
+
 def get_child_nodes(node):
     nodes = []
     for child in node.childNodes:
@@ -354,14 +362,34 @@ class GtkBuilderConverter(object):
             widget.getAttributeNode("constructor").value = parent_id
         node.removeAttribute("id")
 
+def _indent(output):
+    if not subprocess:
+        return output
+
+    for directory in os.environ['PATH'].split(os.pathsep):
+        filename = os.path.join(directory, 'xmllint')
+        if os.path.exists(filename):
+            break
+    else:
+        return output
+
+    s = subprocess.Popen([filename, '--format', '-'],
+                         stdin=subprocess.PIPE,
+                         stdout=subprocess.PIPE)
+    s.stdin.write(output)
+    s.stdin.close()
+    return s.stdout.read()
+
 
 def main():
     if len(sys.argv) < 2:
-        print "Usage: %s filename.glade" % (sys.argv[0],)
-        return
+        raise SystemExit("Usage: %s filename.glade" % (sys.argv[0],))
+
     conv = GtkBuilderConverter()
     conv.parse_file(sys.argv[1])
-    print conv.to_xml()
+
+    xml = conv.to_xml()
+    print _indent(xml)
 
 if __name__ == "__main__":
     main()